<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user extends CI_Controller{
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION["isadmin"]) || !$_SESSION["isadmin"]){
			header("location:/index.php/login");
		}
	}
	/*用户信息列表*/
	public function userlist(){
                $where = '';
                $url = "index.php?c=user&a=userlist";
                $desc = " ORDER BY u.ctime DESC ";
                $userName = isset($_GET['username'])?$_GET['username']:'';
                $data['username'] = $userName;
                $where .= $userName ? " WHERE u.loginName like '%{$userName}%'" : '';
                
                $userID = isset($_GET['userID'])?$_GET['userID']:'';
                $data['userID'] = $userID;
                $where .= $userID ? " WHERE u.id like '%{$userID}%'" : '';
                
                $data['url'] =  $url;
                
                if(isset($_GET['exp']) ) {
                    $desc = " ORDER BY ui.exp ".$_GET['exp'];
                    $data['exp'] = $_GET['exp'];
                    $url .= '&exp='.$_GET['exp'];
                }
                if(isset($_GET['grade']) ) {
                    $desc = " ORDER BY ui.grade ".$_GET['grade'];
                    $data['grade'] = $_GET['grade'];
                    $url .= '&grade='.$_GET['grade'];
                }
                if(isset($_GET['jetton']) ) {
                    $desc = " ORDER BY ui.jetton ".$_GET['jetton'];
                    $data['jetton'] = $_GET['jetton'];
                    $url .= '&jetton='.$_GET['jetton'];
                }
                if(isset($_GET['money']) ) {
                    $desc = " ORDER BY ui.money ".$_GET['money'];
                    $data['money'] = $_GET['money'];
                    $url .= '&money='.$_GET['money'];
                }
                if(isset($_GET['maxPayNO']) ) {
                    $desc = " ORDER BY ui.maxPayNO ".$_GET['maxPayNO'];
                    $data['maxPayNO'] = $_GET['maxPayNO'];
                    $url .= '&maxPayNO='.$_GET['maxPayNO'];
                }
                if(isset($_GET['payNO']) ) {
                    $desc = " ORDER BY ui.payNO ".$_GET['payNO'];
                    $data['payNO'] = $_GET['payNO'];
                    $url .= '&payNO='.$_GET['payNO'];
                }
                if(isset($_GET['victory']) ) {
                    $desc = " ORDER BY ui.victory ".$_GET['victory'];
                    $data['victory'] = $_GET['victory'];
                    $url .= '&victory='.$_GET['victory'];
                }
                if(isset($_GET['failure']) ) {
                    $desc = " ORDER BY ui.failure ".$_GET['failure'];
                    $data['failure'] = $_GET['failure'];
                    $url .= '&failure='.$_GET['failure'];
                }                
                
                $this->load->model("UserModel");
                $this->config->load('pagination',true);
		$pagesize  = $this->config->item("per_page","pagination");
		$sposition  = isset($_GET['per_page']) && (int)$_GET['per_page'] ? (int)$_GET['per_page'] : 0;
                $sql = " SELECT u.id,u.loginName,u.qq_openid,u.sina_openid,u.state,ui.* FROM pk_user AS u LEFT JOIN pk_user_info AS ui ON u.id = ui.userID $where $desc LIMIT $sposition ,$pagesize ";
                $data['userList'] = $this->UserModel->getAll($sql);
                $this->load->library('pagination');
                if( $userName ) $url .= "&username=$userName";
                if( $userID ) $url .= "&userID=$userID";
                $config['base_url'] = $url;
		$config['total_rows'] = $this->UserModel->userCount($where);
                $this->pagination->initialize($config);
		$data['page'] = $this->pagination->create_links();
                
		$this->load->view("header");
		$this->load->view("user/userList",$data);
		$this->load->view("footer");
	}
        
        /*用户状态修改*/
        public function userState(){
            
            $uid = $_GET['uid'];
            $stateTmp = $_GET['state'];
            if( 0 == intval($uid) ) mcmessage_error ("出错了，请重试！");
            $state = 1 == $stateTmp ? "0" : "1";
            $message = 1 == $stateTmp ? "设置用户状态为正常" : "禁用用户";
            $sql = "UPDATE pk_user SET state = '{$state}' WHERE id = '{$uid}'";
            $this->load->model('UserModel');
            if( $this->UserModel->updateState( $sql ) ){
                mcmessage_error( $message.'成功' , $_SERVER['HTTP_REFERER'] );
                exit;
            }else{
                mcmessage_error( $message."失败" , $_SERVER['HTTP_REFERER'] );
                exit;
            }
        }
        /*用户详细信息*/
        public function userDetail(){
            $uid = $_GET['uid'];
            if( 0 == intval($uid) ){
                mcmessage_error( '出错了，请重试' , $_SERVER['HTTP_REFERER'] );
                exit;
            }
            
            $this->load->model('UserModel');
            $sql = " SELECT u.*,ui.* FROM  pk_user AS u LEFT JOIN pk_user_info AS ui ON u.id = ui.userID WHERE u.id = '{$uid}'";
            $data['userInfo'] = $this->UserModel->getRow($sql);
            $this->load->view("header");
            $this->load->view("user/userDetail",$data);
            $this->load->view("footer");    
        }
        
        /*用户道具列表*/
        public function userProps(){
            $uid = $_GET['uid'];
            if( 0 == intval($uid) ){
                mcmessage_error( '出错了，请重试' , $_SERVER['HTTP_REFERER'] );
                exit;
            }
            
            $this->load->model('UserModel');
            $sql = " SELECT * FROM pk_user_props WHERE userID = '{$uid}'";
            $data['propsList'] = $this->UserModel->getAll($sql);
            $this->load->view("header");
            $this->load->view("user/userProps",$data);
            $this->load->view("footer");
        }
        
        /*用户好友列表*/
        public function userFriend(){
            $uid = isset( $_GET['uid']) && 0 < intval($_GET['uid']) ? intval($_GET['uid']) : 0;
            if( 0 == intval($uid) ){
                mcmessage_error( '出错了，请重试' , $_SERVER['HTTP_REFERER'] );
                exit;
            }
            
            $url = "/index.php?c=user&a=userFriend";
            $data['uid'] = $uid;
            $where = "";
            if( $uid ){
                $where  = " WHERE userID = '{$uid}' " ;
                $url.= "&uid=".$uid;
            }
            
            $userID = isset( $_GET['userID']) && 0 < intval($_GET['userID']) ? intval($_GET['userID']) : '';
            $data['userID'] = $userID;
            if( $userID ){
                $where  = " WHERE userID = '{$userID}' " ;
                $url.= "&userID=".$userID;
            }
            
            $sql = " SELECT  * FROM pk_user_friend  $where ";
            $this->load->model('UserModel');
            $data['friendList'] = $this->UserModel->getAll($sql);
            $this->load->library('pagination');
            $config['base_url'] = $url;
            $config['total_rows'] = $this->UserModel->friendCount($where);
            $this->pagination->initialize($config);
            $data['page'] = $this->pagination->create_links();
            
            $this->load->view("header");
            $this->load->view("user/userFriend",$data);
            $this->load->view("footer");
        }
                 
}